package com.zfei.gfb.util;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.zfei.gfb.bean.Status;
import com.zfei.gfb.bean.StatusException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author fgh
 */
public class Beans {

    private static final Logger logger = LoggerFactory.getLogger(Beans.class);

    public static <T> T copy(Object source, Class<T> clazz) {
        return copy(source, clazz, (String[]) null);
    }

    public static <T, K> List<T> copys(List<K> sources, Class<T> clazz) {
        return sources.stream().map(source -> copy(source, clazz)).collect(Collectors.toList());
    }

    public static <T> T copy(Object source, Class<T> clazz, String... ignoreProperties) {
        if (source != null) {
            try {
                T target = clazz.newInstance();
                BeanUtils.copyProperties(source, target, ignoreProperties);
                return target;
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }
        return null;
    }


    public static String[] getNullPropertyNames(Object source) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

        Set<String> emptyNames = new HashSet<String>();
        for (java.beans.PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null) {
                emptyNames.add(pd.getName());
            }
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }

    public static void copyPropertiesIgnoreNull(Object src, Object target) {
        BeanUtils.copyProperties(src, target, getNullPropertyNames(src));
    }

    public static JsonElement getJsonData(String result, String key) {
        return getJsonData(result, key, false);

    }

    public static JsonElement getJsonData(String result, String key, boolean data) {
        JsonObject json = Gsons.parseJsonObject(result);
        JsonElement jsonElement = json.get("code");
        if (jsonElement == null) {
            logger.error("remote return error,result ={}", result);
            throw new StatusException(Status.HTTP_REMOTE_ERROR, "code null");
        }
        int asInt = jsonElement.getAsInt();
        if (asInt != 200) {
            logger.error("remote return error,result ={}", result);
            throw new StatusException(Status.HTTP_REMOTE_ERROR, "code !=200");
        }
        if (data) {
            return json.get("data");
        }
        JsonObject dataElement = json.getAsJsonObject("data");
        if (dataElement == null) {
            logger.error("remote return error,result ={}", result);
            throw new StatusException(Status.HTTP_REMOTE_ERROR, "data null");
        }

        JsonElement targetElement = dataElement.get(key);
        if (targetElement == null) {
            logger.error("remote return error,result ={}", result);
            throw new StatusException(Status.HTTP_REMOTE_ERROR, "data->key null");
        }
        return targetElement;

    }


}
